package 剑指offer;


import static 剑指offer.岛屿数量.toCharArr;

/**
 * @description: 给定一个由0和1组成的2维矩阵，返回该矩阵中最大的由1组成的正方形的面积
 * @author: ywk
 * @date: 2021-02-20
 */
public class 最大正方形 {


    public static int solve(char[][] matrix) {
        int maxSlide = 0;
        int[][] dp = new int[matrix.length][matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == '1') {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]),
                                dp[i - 1][i - 1]) + 1;
                    }
                }
                maxSlide = Math.max(maxSlide, dp[i][j]);
            }
        }
        return maxSlide * maxSlide;
    }


    public static void main(String[] args) {
        System.out.println(solve(toCharArr(new int[][]{
                {1, 0, 1, 1, 1, 1, 1, 0, 0, 0},
                {1, 0, 1, 1, 1, 0, 0, 0, 0, 0},
                {1, 0, 1, 0, 1, 1, 1, 0, 0, 0},
                {1, 1, 0, 1, 1, 1, 1, 0, 1, 0},
                {1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
                {1, 0, 1, 1, 1, 1, 1, 1, 1, 0},
                {1, 0, 1, 1, 1, 1, 1, 1, 1, 0},
                {1, 0, 1, 1, 1, 1, 1, 1, 1, 0},
                {1, 1, 0, 1, 1, 1, 1, 1, 0, 0},
                {1, 0, 1, 1, 0, 1, 1, 0, 0, 0}})));
    }
}
